package com.jiazhong.springmvc.test01;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Auther:shuXin
 */
@Controller
@RequestMapping("cookie")
@ResponseBody
public class CookieController {

    //往cookie中存入数据
    @RequestMapping("save")
    public String save(HttpServletResponse response) {
        Cookie cookie = new Cookie("username", "zhangsan");
        //将cookie加载到响应当中
        response.addCookie(cookie);
        return "OK";
    }

    @RequestMapping("getValue")
    public String getValue(@CookieValue(value = "username") String username){
        System.out.println(username);
        return username;
    }
}
